import { Component, OnInit, Input } from '@angular/core'
import {
  IChargeBack,
  EChargeBackProcessType,
  EChargeBackTransCode,
  ChargeBackTransCodeMap,
  ChargeBackProcessTypeMap,
} from '@shared/interfaces/operation'
import { IBaseInfoColumn } from '@shared/interfaces/baseInfo'
import { DetailBaseClass } from '@shared/classes/DetailBaseClass'
import { I18NService } from '@core'
import { Router, ActivatedRoute } from '@angular/router'
import { PageService } from '@shared/services/page.service'
import { AmtFormatPipe } from '@shared/filters/amt-format.pipe'

@Component({
  selector: 'app-task-charge-trade-detail',
  templateUrl: './charge-trade-detail.component.html',
})
export class ChargeBackTradeDetailComponent extends DetailBaseClass<IChargeBack> implements OnInit {
  firstColumns: IBaseInfoColumn[]
  secondColumns: IBaseInfoColumn[]
  thirdColumns: IBaseInfoColumn[]
  @Input() detail: IChargeBack

  constructor(
    public i18n: I18NService,
    public router: Router,
    public route: ActivatedRoute,
    public pageService: PageService,
    private amtFormatPipe: AmtFormatPipe,
  ) {
    super(pageService)
  }

  ngOnInit() {
    this.detailData = this.detail
    this.initFirstColumns()
    this.initSecondColumns()
    this.initThirdColumns()
    this.initColumns()
  }

  initColumns(): void {
    this.initValue.call(this, this.firstColumns)
    this.initValue.call(this, this.secondColumns)
    this.initValue.call(this, this.thirdColumns)
  }

  initFirstColumns(): void {
    this.firstColumns = [
      {
        span: 6,
        index: 'transDateTime',
        label: '差错日期',
        labelI18n: 'OPERATION.CHARGEBACK.DATE',
      },
      {
        span: 6,
        index: 'transAmt',
        label: '差错金额',
        labelI18n: 'TRADE.CHARGEBACK.AMT',
        asyncValue: this.amtFormatPipe.transform(this.detailData.transAmt, this.detail.transCurr),
      },
      {
        span: 6,
        index: 'acqReferData',
        label: 'ARN',
      },
      {
        span: 6,
        index: 'transCode',
        label: '差错类型',
        labelI18n: 'OPERATION.CHARGEBACK.TRANSCODE',
        displayValue: this.parseTransCode(this.detailData.transCode),
      },
    ]
  }

  initSecondColumns(): void {
    switch (this.detailData.processType) {
      case EChargeBackProcessType.Close:
      case EChargeBackProcessType.Retrievaled:
      case EChargeBackProcessType.Arbitrate:
        this.showRemarkOnlyInSecondColumns()
        break
      case EChargeBackProcessType.MerchantDebit:
        this.showDebitAmtInSecondColumns()
        break
      case EChargeBackProcessType.Representment:
      case EChargeBackProcessType.DebitAndRepresent:
        this.showMultiParamsInSecondColumns()
        break
    }
  }

  showRemarkOnlyInSecondColumns(): void {
    this.secondColumns = [
      {
        span: 12,
        index: 'processType',
        label: '处理方式',
        labelI18n: 'OPERATION.CHARGEBACK.HANDLEMETHOD',
        displayValue: this.parseProcessType(this.detailData.processType),
      },
      {
        span: 12,
        index: 'operator',
        label: '操作用户',
        labelI18n: 'OPERATION.HOLDRELEASE.DETAIL.OPERATOR',
      },
    ]
  }

  showDebitAmtInSecondColumns(): void {
    this.secondColumns = [
      {
        span: 12,
        label: '处理方式',
        labelI18n: 'OPERATION.CHARGEBACK.HANDLEMETHOD',
        displayValue: this.parseProcessType(this.detailData.processType),
      },
      {
        span: 12,
        index: 'operator',
        label: '操作用户',
        labelI18n: 'OPERATION.HOLDRELEASE.DETAIL.OPERATOR',
      },
    ]
  }

  showMultiParamsInSecondColumns(): void {
    this.secondColumns = [
      {
        span: 6,
        index: 'processType',
        label: '处理方式',
        labelI18n: 'OPERATION.CHARGEBACK.HANDLEMETHOD',
        displayValue: this.parseProcessType(this.detailData.processType),
      },
      {
        span: 6,
        index: 'operator',
        label: '操作用户',
        labelI18n: 'OPERATION.HOLDRELEASE.DETAIL.OPERATOR',
      },
      {
        span: 6,
        index: 'reasonCode',
        label: '原因代码',
        labelI18n: 'OPERATION.CHARGEBACK.REASONCODE',
      },
      {
        span: 6,
        index: 'memMessTxt',
        label: 'MMT',
        labelI18n: 'OPERATION.CHARGEBACK.MMT',
      },
    ]
  }

  initThirdColumns(): void {
    switch (this.detailData.processType) {
      case EChargeBackProcessType.DebitAndRepresent:
        this.showDebitAndRepresentThirdColumns()
        break
      default:
        this.showOtherThirdColumns()
        break
    }
  }

  showDebitAndRepresentThirdColumns(): void {
    this.thirdColumns = [
      {
        span: 8,
        index: 'downTransAmt',
        label: '扣款金额',
        asyncValue: this.amtFormatPipe.transform(this.detailData.downTransAmt, this.detailData.downTransCurr),
        labelI18n: 'OPERATION.CHARGEBACK.DEBITAMT',
      },
      {
        span: 8,
        index: 'upTransAmt',
        label: '再请款金额',
        asyncValue: this.amtFormatPipe.transform(this.detailData.upTransAmt, this.detailData.upTransCurr),
        labelI18n: 'OPERATION.CHARGEBACK.REPRESENTAMT',
      },
      {
        span: 8,
        index: 'remark',
        label: '备注',
        labelI18n: 'GENERAL.INFO.REMARK',
      },
    ]
  }

  showOtherThirdColumns(): void {
    this.thirdColumns = [
      {
        span: 24,
        index: 'remark',
        label: '备注',
        labelI18n: 'GENERAL.INFO.REMARK',
      },
    ]
  }

  parseTransCode(transCode: EChargeBackTransCode): string {
    return this.trans(ChargeBackTransCodeMap[transCode])
  }

  parseProcessType(processType: EChargeBackProcessType): string {
    return this.trans(ChargeBackProcessTypeMap[processType])
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
